package com.carbon.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carbon.domain.Mark;
import com.carbon.service.MarkService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

import com.carbon.utils.OrderIdUtil;
import com.carbon.vo.SearchParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.carbon.common.core.domain.AjaxResult;
/**
 * 招标信息发布(Mark)表控制层
 *
 * @author WangFK
 * @since 2023-09-11 16:46:01
 */
@RestController
@Slf4j
@RequestMapping("/wms/mark")
public class MarkController {
    /**
     * 服务对象
     */
    @Resource
    private MarkService markService;

    @PutMapping("/changeMarkStatus")
    public AjaxResult changeMarkStatus(String id,String markStatus){
        LambdaUpdateWrapper<Mark> lqw = new LambdaUpdateWrapper<Mark>()
                .eq(Mark::getId, id)
                .set(Mark::getMarkStatus, markStatus);
        return markService.update(lqw)?AjaxResult.success():AjaxResult.error();
    }

    @GetMapping("/list")
    public AjaxResult list(SearchParams params){
        Page<Mark> markPage = new Page<>(params.getPageNum(), params.getPageSize());
        LambdaUpdateWrapper<Mark> lqw = new LambdaUpdateWrapper<Mark>()
                .orderByAsc(Mark::getCreateTime)
                .orderByAsc(Mark::getMarkEnd);
        String search = params.getSearch();
        String dateTime = params.getDateTime();
        if (ObjectUtil.isNotEmpty(search)){
            lqw.like(Mark::getCreateBy,search).or()
                    .like(Mark::getMarkName,search).or()
                    .like(Mark::getMarkId,search);
        }
        if (ObjectUtil.isNotEmpty(dateTime)){
            lqw.like(Mark::getCreateTime,dateTime).or()
                    .like(Mark::getMarkEnd,dateTime).or()
                    .like(Mark::getUpdateTime,dateTime);
        }
        markPage = markService.page(markPage, lqw);
        List<Mark> records = markPage.getRecords();
        return AjaxResult.success()
                .put("rows",records)
                .put("total",markPage.getTotal());

    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult selectOne(@PathVariable Serializable id) {
        return AjaxResult.success(markService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param mark 实体对象
     * @return 新增结果
     */
    @PostMapping
    public AjaxResult insert(@RequestBody Mark mark) {

        if (ObjectUtil.isEmpty(mark.getMarkName())){
            return AjaxResult.error("招标名不为空");
        }
        String markEnd = mark.getMarkEnd();
        if (ObjectUtil.isEmpty(markEnd)){
            return AjaxResult.error("结束招标时间为空");
        }
        String today = DateUtil.today();
        if (today.compareToIgnoreCase(markEnd)>0){
            return AjaxResult.error("结束时间: "+markEnd+" 不可早于今天时间:"+today);
        }
        if (ObjectUtil.isNotEmpty(mark.getId())){
            return update(mark);
        }
        LambdaUpdateWrapper<Mark> lqw = new LambdaUpdateWrapper<Mark>()
                .eq(Mark::getMarkName, mark.getMarkName());
        if (ObjectUtil.isNotEmpty(markService.list(lqw))){
            return AjaxResult.error("招名: "+mark.getMarkName()+"重复");
        }
        mark.setMarkId(OrderIdUtil.getOrderId());
        return markService.save(mark)?AjaxResult.success():AjaxResult.error();
    }

    /**
     * 修改数据
     *
     * @param mark 实体对象
     * @return 修改结果
     */
    @PutMapping
    public AjaxResult update(@RequestBody Mark mark) {
        Mark byId = markService.getById(mark.getId());
        if (!byId.getMarkName().equalsIgnoreCase(mark.getMarkName())){
            LambdaUpdateWrapper<Mark> lqw = new LambdaUpdateWrapper<Mark>()
                    .eq(Mark::getMarkName, mark.getMarkName());
            if (ObjectUtil.isNotEmpty(markService.list(lqw))){
                return AjaxResult.error("招名: "+mark.getMarkName()+"重复");
            }
        }
        return markService.updateById(mark)?AjaxResult.success():AjaxResult.error();

    }

    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable String id) {
        List<String> split = StrUtil.split(id, ",");
        LambdaUpdateWrapper<Mark> in = new LambdaUpdateWrapper<Mark>()
                .in(Mark::getId, split);
        return markService.remove(in)?AjaxResult.success():AjaxResult.error();
    }
}

